const Client = require('../model/article.model')
const ArticleType = require('../model/article_type.model')
const { Op, Sequelize } = require("sequelize");
class ArticleTypeService {
    // add
    async addArticleTypeService(param) {
        const {type} = param
        const res = await ArticleType.create({ type,is_delete:0 })
        return res;
    }
    // update delete
    async updateArticleService(param) {
        const { type, new_type, is_delete } = param
        let obj ={}
        is_delete && (obj['is_delete'] = is_delete)
        new_type && (obj['new_type'] = new_type)
        console.log(obj)
        const res = await ArticleType.update(obj, {
            where: {
                type: type
            }
        })
        console.log(res)
        return res[0] > 0 ? 'success' : 'error'
    }
    // get
    async getArticleService(param) {
        const {currentPage, pageSize,type,createdFrom,createdTo} = param
        console.log(currentPage, pageSize,type,createdFrom,createdTo)
        const where = {is_delete : 0}
        createdTo && (where['createdAt'] = {
            [Op.between]: [createdFrom, createdTo]
        })
        type && (where['type'] = type)
        const { count, rows } = await ArticleType.findAndCountAll({
            offset: (currentPage - 1) * pageSize,
            limit: Number(pageSize),
            where
        });
        console.log(rows)
        return rows ? { data: rows, total: count } : { error: 500 }

    }
}
// 导出后 在控制器controller中使用
module.exports = new ArticleTypeService()